package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfTradeFileCustomsInfoService.java
* Author:   you.yichao
* Date:     2019-10-11
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-10-11          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mdb.bc.api.data.ListResultEx;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.cache.sequence.util.SequenceGenerator;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.open.common.enums.TfSequenceEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.bo.trade.SaveTradeFileCustomsInfoBo;
import com.mdb.open.trade.domain.bo.trade.UpdateTradeFileCustomsInfoBo;
import com.mdb.open.trade.domain.po.trade.TradeFileCustomsInfo;
import com.mdb.open.trade.mapper.trade.TfTradeFileCustomsInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *  贸易档案报关信息表 服务类
 *
 * @author you.yichao
 * @date 2019-10-11
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfTradeFileCustomsInfoService extends SuperService<TfTradeFileCustomsInfoMapper, TradeFileCustomsInfo> {

    @Autowired
    SequenceGenerator sequenceGenerator;

    public ResultEx<TradeFileCustomsInfo> getInfoByNo(String tradeFileNo) {

        return ResultEx.ok(this.baseMapper.selectOne(new LambdaQueryWrapper<TradeFileCustomsInfo>()
                .eq(TradeFileCustomsInfo::getTradeFileNo, tradeFileNo)));
    }

    public ListResultEx<TradeFileCustomsInfo> queryInfoByNo(String tradeFileNo) {
        return ListResultEx.ok(this.baseMapper.selectList(new LambdaQueryWrapper<TradeFileCustomsInfo>()
                .eq(TradeFileCustomsInfo::getTradeFileNo, tradeFileNo)
                .eq(TradeFileCustomsInfo::isDeleted, false)));
    }

    public ResultEx<TradeFileCustomsInfo> saveCustomsInfo(SaveTradeFileCustomsInfoBo bo) {

        TradeFileCustomsInfo customsInfo = new TradeFileCustomsInfo();
        customsInfo.setTradeFileNo(bo.getTradeFileNo());
        customsInfo.setTradeFileCustomsNo(sequenceGenerator.generator(TfSequenceEnum.TRADE_FILE_CUSTOMS_NO));
        customsInfo.setBusinessNo(bo.getBusinessNo());
        customsInfo.setCustomerCode(bo.getCustomerCode());
        customsInfo.setMcustomerCode(bo.getMcustomerCode());
        customsInfo.setCustomerName(bo.getCustomerName());
        customsInfo.setMerchantNo(bo.getMerchantNo());
        customsInfo.setCustomsNo(bo.getCustomsNo());
        customsInfo.setMerchantName(bo.getMerchantName());
        customsInfo.setCustomsLocation(bo.getCustomsLocation());
        customsInfo.setCustomsTitle(bo.getCustomsTitle());
        customsInfo.setCustomsAmount(bo.getCustomsAmount());
        customsInfo.setCcy(bo.getCcy());
        customsInfo.setCommitTime(bo.getCommitTime());
        customsInfo.setLoadingPort(bo.getLoadingPort());
        customsInfo.setLoadingPortCode(bo.getLoadingPortCode());
        customsInfo.setDestinPort(bo.getDestinPort());
        customsInfo.setDestinPortCode(bo.getDestinPortCode());
        customsInfo.setDestinCountry(bo.getDestinCountry());
        customsInfo.setSourceType(bo.getSourceType());

        int count = this.baseMapper.insert(customsInfo);
        if (count != 1) {
            return ResultEx.fail();
        }
        return ResultEx.ok(customsInfo);
    }

    public ResultEx updateCustomsInfo(UpdateTradeFileCustomsInfoBo bo) {

        TradeFileCustomsInfo updateCustomsInfo = new TradeFileCustomsInfo();
        updateCustomsInfo.setCustomsNo(bo.getCustomsNo());
        updateCustomsInfo.setCustomsLocation(bo.getCustomsLocation());
        updateCustomsInfo.setCustomsTitle(bo.getCustomsTitle());
        updateCustomsInfo.setCustomsAmount(bo.getCustomsAmount());
        updateCustomsInfo.setCcy(bo.getCcy());
        updateCustomsInfo.setCommitTime(bo.getCommitTime());
        updateCustomsInfo.setLoadingPort(bo.getLoadingPort());
        updateCustomsInfo.setLoadingPortCode(bo.getLoadingPortCode());
        updateCustomsInfo.setDestinPort(bo.getDestinPort());
        updateCustomsInfo.setDestinPortCode(bo.getDestinPortCode());
        updateCustomsInfo.setDestinCountry(bo.getDestinCountry());

        LambdaUpdateWrapper<TradeFileCustomsInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TradeFileCustomsInfo::getTradeFileNo, bo.getTradeFileNo());
        updateWrapper.eq(TradeFileCustomsInfo::getTradeFileCustomsNo, bo.getTradeFileCustomsNo());

        int count = this.baseMapper.update(updateCustomsInfo, updateWrapper);
        if (count != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

    public ResultEx removeCustomsInfo(UpdateTradeFileCustomsInfoBo bo) {

        LambdaUpdateWrapper<TradeFileCustomsInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TradeFileCustomsInfo::getTradeFileNo, bo.getTradeFileNo());
        updateWrapper.eq(TradeFileCustomsInfo::getTradeFileCustomsNo, bo.getTradeFileCustomsNo());

        int count = this.baseMapper.delete(updateWrapper);
        if (count != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }


}
